import wx
import wx.media
#import wx.lib.agw.gradientbutton as GB
from wx.lib.agw.shapedbutton import SBitmapToggleButton
try:
    from . import image
except ImportError:
    import image




class Book_media(wx.Panel):
    def __init__(self, parent, file_name, media_type, **k):
        wx.Panel.__init__(self, parent, **k)
        self.progress = True
        sizer = wx.BoxSizer( wx.VERTICAL )
        self.media_ctrl = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER, szBackend="")
        sizer.Add( self.media_ctrl, 1, wx.EXPAND, 5 )
        m_Hsizer = wx.BoxSizer( wx.HORIZONTAL )
        m_Vsizer = wx.BoxSizer( wx.VERTICAL )

        self.status_button = SBitmapToggleButton(self, -1, image._media_paly.GetBitmap())
        self.status_button.SetUseFocusIndicator(False)
        self.m_slider1 = wx.Slider( self, value=0, minValue=0, maxValue=100, style=wx.SL_VALUE_LABEL)
        m_Vsizer.Add(self.m_slider1, 1, wx.EXPAND, 5 )
        m_Hsizer.Add( self.status_button, 1, wx.ALL | wx.EXPAND, 5 )
        m_Vsizer.Add( m_Hsizer, 0, wx.EXPAND, 5 )
        sizer.Add( m_Vsizer, 0, wx.EXPAND, 5 )
        
        self.Bind(wx.media.EVT_MEDIA_STOP, self.on_media_status)
        self.m_slider1.Bind(wx.EVT_LEFT_DOWN, self.on_mouse_progress)
        self.m_slider1.Bind(wx.EVT_LEFT_UP, self.on_mouse_progress)
        self.status_button.Bind(wx.EVT_BUTTON, self.on_button)
        self.m_slider1.Bind(wx.EVT_SCROLL_CHANGED, self.on_set_progress_bar)
        self.status_button.Enable(False)

        self.SetSizer(sizer)

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.on_Timer)
        self.timer.Start(500)
        ensic = [wx.AcceleratorEntry(wx.ACCEL_NORMAL, 32, self.status_button.GetId())]
        self.SetAcceleratorTable(wx.AcceleratorTable(ensic))
        self.on_create(file_name)
        #self.media_ctrl.ShowPlayerControls(wx.media.MEDIACTRLPLAYERCONTROLS_DEFAULT)
        self.Bind(wx.EVT_CLOSE, self.on_close)

    def on_close(self, eve):
        self.timer.Stop()
        self.Destroy()

    def on_create(self, file_name):
        self.media_ctrl.Load(file_name)
        self.media_ctrl.SetInitialSize()
        self.GetSizer().Layout()

    def on_Timer(self, eve):
        if self.progress:
            offset = self.media_ctrl.Tell()
            self.m_slider1.SetValue(offset)

    def on_media_status(self, eve):
        self.status_button.Enable(True)
        if not self.status_button.GetValue():
            self.m_slider1.SetRange(0, self.media_ctrl.Length())
        self.status_button.SetValue(True)
        self.status_button.SetBitmapLabel(image._media_paly.GetBitmap())

    def on_mouse_progress(self, eve):
        if eve.EventType == wx.EVT_LEFT_DOWN.typeId:
            self.progress = False
        elif eve.EventType == wx.EVT_LEFT_UP.typeId:
            self.progress = True
        eve.Skip()

    def on_set_progress_bar(self, eve):
        offset = self.m_slider1.GetValue() - self.media_ctrl.Tell()
        self.media_ctrl.Seek(offset, wx.FromCurrent)

    def on_button(self, eve):
        if not eve.GetEventObject().GetValue():
            eve.GetEventObject().SetValue(False)
            eve.GetEventObject().SetBitmapLabel(image._media_pause.GetBitmap())
            self.media_ctrl.Play()
        else:
            self.media_ctrl.Pause()
            eve.GetEventObject().SetBitmapLabel(image._media_paly.GetBitmap())
            eve.GetEventObject().SetValue(True)




def main():
    app = wx.App()
    ex = wx.Frame(None)
    Book_media(ex, "ts.ts", 69)
    ex.Show()
    app.MainLoop()


if __name__ == '__main__':
    main()